package com.dynamicallo.ty.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dynamicallo.ty.entity.Content;
import com.dynamicallo.ty.service.IContentService;

/**
 * springMVC学习
 * 
 * @author wb
 * @date 2016年1月2日 下午10:15:55
 */
@Scope("prototype")
@Controller
@RequestMapping("/products")
public class TestController {
    @Autowired
    private IContentService contentService;
    /**
     * 测试方法,HelloWord
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String getProducts(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //http://localhost:8080/DynamicAllocationSSM/products/list
        System.out.println("访问成功");
        request.setAttribute("name", "helloWord");

        return "products/list";

    }

    @RequestMapping(value = "/info/{proId}", method = RequestMethod.GET)
    public String getProductInfo(@PathVariable String proId, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //
        System.out.println("访问成功");
        request.setAttribute("name", proId);

        return "products/list";

    }
    /** 
     * 测试返回JSON数据 
     * @param session 
     * @return 
     */  
    @RequestMapping(value="/test",produces="text/html;charset=UTF-8")  
    @ResponseBody  
    public Object test(HttpSession session){  
          
        System.out.println("test....................");  
        return contentService.getAllContent();  
    }  
    /** 
     * 测试返回JSON数据 
     * @param session 
     * @return 
     */  
    @RequestMapping(value="/test2",produces="text/html;charset=UTF-8")  
    @ResponseBody  
    public void test2(HttpSession session,HttpServletRequest request, HttpServletResponse response){  
        response.setContentType("text/json;charset=UTF-8");  
        response.setCharacterEncoding("UTF-8");  
        System.out.println("test2....................");  
        List<Content> temp =contentService.getAllContent();  
        
        try {
            response.getWriter().print(JSON.toJSONString(temp));
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
    }
}
